package com.bianmaba.batch.example.db.batch;


import com.bianmaba.batch.example.db.bean.Log;
import org.springframework.batch.item.validator.ValidatingItemProcessor;
import org.springframework.batch.item.validator.ValidationException;
import org.springframework.batch.item.validator.Validator;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * @program: spring-batch-examples
 * @description:
 * @author: Chenjiabin
 * @create: 2019/1/8 11:30
 **/
@Service
public class CsvItemProcessor extends ValidatingItemProcessor<Log> {
    public CsvItemProcessor(@Autowired CsvBeanValidator validator) {
        super(validator);
    }

    @Override
    public Log process(Log log) {
        super.process(log);
        return log;
    }

    @Service
    public static class CsvBeanValidator<T> implements Validator<T>, InitializingBean {

        private javax.validation.Validator validator;

        @Override
        public void validate(T value) throws ValidationException {
            /**
             * 使用Validator的validate方法校验数据
             */
            Set<ConstraintViolation<T>> constraintViolations =
                    validator.validate(value);
            if (constraintViolations.size() > 0) {
                StringBuilder message = new StringBuilder();
                for (ConstraintViolation<T> constraintViolation : constraintViolations) {
                    message.append(constraintViolation.getMessage() + "\n");
                }
                throw new ValidationException(message.toString());
            }
        }

        /**
         * 使用JSR-303的Validator来校验我们的数据，在此进行JSR-303的Validator的初始化
         *
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            ValidatorFactory validatorFactory =
                    Validation.buildDefaultValidatorFactory();
            validator = validatorFactory.usingContext().getValidator();
        }
    }
}
